$(function() {
    let layer = layui.layer
    let form = layui.form
    let laypage = layui.laypage
    template.defaults.imports.dataForm = function(date) {
        let dt = new Date(date),
            y = padZero(dt.getFullYear()),
            m = padZero(dt.getMonth() + 1),
            d = padZero(dt.getDate()),
            h = padZero(dt.getHours()),
            f = padZero(dt.getMinutes()),
            s = padZero(dt.getSeconds());
        return y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
    }

    function padZero(n) {
        return n < 10 ? '0' + n : n
    }
    let q = {
        pagenum: 1, //页码值
        pagesize: 2, //显示几条
        cate_id: '', //文章Id
        state: '', //发布状态
    }
    initTable()
    initCate()

    function initTable() {
        $.ajax({
            type: "get",
            url: "/my/article/list",
            data: q,
            success: function(res) {
                // console.log(res);
                if (res.status) {
                    return layer.msg("获取文章列表失败！")
                }
                let htmlStr = template("tpl-table", res)
                $("tbody").html(htmlStr)
                renderPage(res.total)
            }
        });
    }

    function initCate() {
        $.ajax({
            type: "get",
            url: "/my/article/cates",
            success: function(res) {
                // console.log(res);
                if (res.status) {
                    return layer.msg("获取分类数据失败！")
                }
                let htmlStr = template("tpl-cate", res)
                $("[name=cate_id]").html(htmlStr)
                form.render()
            }
        });
    }
    $("#form-search").on("submit", function(e) {
        e.preventDefault()
        let cate = $("[name=cate_id]").val(),
            state = $("[name=state]").val();
        q.cate_id = cate;
        q.state = state
        initTable()
    });
    // 定义渲染分页的方法


    function renderPage(total) {
        laypage.render({
            elem: 'pageBox',
            count: total, //数据总数，从服务端得到
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function(obj, first) {
                q.pagenum = obj.curr
                q.pagesize = obj.limit
                    //首次不执行
                if (!first) {
                    initTable()
                }
            }
        });
    }
    $("tbody").on("click", ".btn-delete", function() {
        let id = $(this).attr("data-id");
        let del = $(".btn-delete").length
        layer.confirm('确认删除?', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                type: "get",
                url: "/my/article/delete/" + id,
                success: function(res) {
                    if (res.status) {
                        return layer.msg("删除文章失败！")
                    }
                    layer.msg("删除文章成功！");
                    if (del === 1) {
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1
                    }
                    initTable()
                }
            });

            layer.close(index);
        });
    })
})